﻿using Common;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.FallRiskAssessment
{
    public partial class AddFallRiskAssessment : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        PatientInfo Patientinfo;
        int Sum = 0;
        string Id = string.Empty;
        #endregion

        #region 窗体构造函数
        public AddFallRiskAssessment(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            this.Patientinfo = patientInfo;
            this.LoginUser = loginUser;
            this.Id = id;

            //绑定数据
            BindInfo(Id);

            //加载电签
            ElectronicSignature();
        }
        public AddFallRiskAssessment(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量赋值
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;

            //绑定数据
            BindInfo(this.Id);

            //加载电签
            ElectronicSignature();
        }
        public AddFallRiskAssessment(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = id;

            //绑定数据
            BindInfo(Id);

            //加载电签
            ElectronicSignature();
        }
        /// <summary>
        /// 初始化加载印章图片
        /// </summary>
        private void ElectronicSignature()
        {
            ElectronicSignature es = new ElectronicSignature(LoginUser);
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, textpingguren, picElectronicSignature, textpingguren.Location);
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            textpingguren.Text = LoginUser.UserName;
            dateEditpingguriqi.EditValue = DateTime.Now; //默认显示当前日期 
            textzongfen.Text = "0";
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            Fallfar ps = new Fallfar();
            ps = XmlUtil.XMLToObject<Fallfar>(NursingFormData, Encoding.UTF8);
            dateEditpingguriqi.DateTime = Convert.ToDateTime(ps.RiQi.ToString());//评估日期
            textzongfen.Text = ps.ZongDeFen.ToString();//总分
            //年龄
            string[] NianLin = ps.NianLing.Split('#');
            checknl1.Checked = (NianLin[0] == "1");
            checknl2.Checked = (NianLin[1] == "1");
            checknl3.Checked = (NianLin[2] == "1");
            checknl4.Checked = (NianLin[3] == "1");
            //性别
            string[] XingBie = ps.XingBie.Split('#');
            checkxb1.Checked = (XingBie[0] == "1");
            checkxb2.Checked = (XingBie[1] == "1");
            //诊断
            string[] ZhenDuan = ps.ZhenDuan.Split('#');
            checkzd1.Checked = (ZhenDuan[0] == "1");
            checkzd2.Checked = (ZhenDuan[1] == "1");
            checkzd3.Checked = (ZhenDuan[2] == "1");
            checkzd4.Checked = (ZhenDuan[3] == "1");
            //认知障碍
            string[] RenZhiZhangAi = ps.RenZhiZhangAi.Split('#');
            checkrz1.Checked = (RenZhiZhangAi[0] == "1");
            checkrz2.Checked = (RenZhiZhangAi[1] == "1");
            checkrz3.Checked = (RenZhiZhangAi[2] == "1");
            //环境因素
            string[] HuanJingYinSu = ps.HuanJingYinSu.Split('#');
            checkhj1.Checked = (HuanJingYinSu[0] == "1");
            checkhj2.Checked = (HuanJingYinSu[1] == "1");
            checkhj3.Checked = (HuanJingYinSu[2] == "1");
            checkhj4.Checked = (HuanJingYinSu[3] == "1");
            //对手术镇静麻醉的反应
            string[] DuiShouShuZhenJingMaZuiDeFanYing = ps.DuiShouShuZhenJingMaZuiDeFanYing.Split('#');
            checkmz1.Checked = (DuiShouShuZhenJingMaZuiDeFanYing[0] == "1");
            checkmz2.Checked = (DuiShouShuZhenJingMaZuiDeFanYing[1] == "1");
            checkmz3.Checked = (DuiShouShuZhenJingMaZuiDeFanYing[2] == "1");
            //药物应用
            string[] YaoWuYingYong = ps.YaoWuYingYong.Split('#');
            checkyw1.Checked = (YaoWuYingYong[0] == "1");
            checkyw2.Checked = (YaoWuYingYong[1] == "1");
            checkyw3.Checked = (YaoWuYingYong[2] == "1");
            textpingguren.Text = ps.PingGuRen;//评估人
        }
        #endregion

        #region 取消
        private void simpleButtonquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region   计算总分
        public void SunTotal()
        {
            int Total = 0;
            int Total1 = 0;
            int Total2 = 0;
            int Total3 = 0;
            int Total4 = 0;
            int Total5 = 0;
            int Total6 = 0;

            //年龄
            if (checknl1.Checked)
                Total += 4;
            else
                Total += 0;

            if (checknl2.Checked)
                Total += 3;
            else
                Total += 0;
            if (checknl3.Checked)
                Total += 2;
            else
                Total += 0;

            if (checknl4.Checked)
                Total += 1;
            else
                Total += 0;

            //性别
            if (checkxb1.Checked)
                Total1 += 2;
            else
                Total1 += 0;

            if (checkxb2.Checked)
                Total1 += 1;
            else
                Total1 += 0;
            //诊断
            if (checkzd1.Checked)
                Total2 += 4;
            else
                Total2 += 0;

            if (checkzd2.Checked)
                Total2 += 3;
            else
                Total2 += 0;
            if (checkzd3.Checked)
                Total2 += 2;
            else
                Total2 += 0;

            if (checkzd4.Checked)
                Total2 += 1;
            else
                Total2 += 0;

            //认知障碍
            if (checkrz1.Checked)
                Total3 += 3;
            else
                Total3 += 0;
            if (checkrz2.Checked)
                Total3 += 2;
            else
                Total3 += 0;
            if (checkrz3.Checked)
                Total3 += 1;
            else
                Total3 += 0;

            //环境因素
            if (checkhj1.Checked)
                Total4 += 4;
            else
                Total4 += 0;
            if (checkhj2.Checked)
                Total4 += 3;
            else
                Total4 += 0;
            if (checkhj3.Checked)
                Total4 += 2;
            else
                Total4 += 0;
            if (checkhj4.Checked)
                Total4 += 1;
            else
                Total4 += 0;

            //对手术镇静麻醉的反应
            if (checkmz1.Checked)
                Total5 += 3;
            else
                Total5 += 0;

            if (checkmz2.Checked)
                Total5 += 2;
            else
                Total5 += 0;
            if (checkmz3.Checked)
                Total5 += 1;
            else
                Total5 += 0;

            //药物反应
            if (checkyw1.Checked)
                Total6 += 3;
            else
                Total6 += 0;
            if (checkyw2.Checked)
                Total6 += 2;
            else
                Total6 += 0;
            if (checkyw3.Checked)
                Total6 += 1;
            else
                Total6 += 0;

            Sum = Total + Total1 + Total2 + Total3 + Total4 + Total5 + Total6;
            textzongfen.Text = Sum.ToString();
        }
        #endregion

        #region  总分
        private void checknl1_CheckedChanged(object sender, EventArgs e)
        {
            SunTotal();
        }
        #endregion

        #region 保存
        private void simpleButtonbaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateEditpingguriqi.Text))
                dateEditpingguriqi.DateTime = DateTime.Now;

            Fallfar ff = new Fallfar();
            //日期
            ff.RiQi = dateEditpingguriqi.Text + " " + DateTime.Now.ToString("HH:mm:ss");
            //年龄
            ff.NianLing = (checknl1.Checked ? 1 : 0) + "#" + (checknl2.Checked ? 1 : 0) + "#" + (checknl3.Checked ? 1 : 0) + "#" + (checknl4.Checked ? 1 : 0);
            //性别
            ff.XingBie = (checkxb1.Checked ? 1 : 0) + "#" + (checkxb2.Checked ? 1 : 0);
            //诊断
            ff.ZhenDuan = (checkzd1.Checked ? 1 : 0) + "#" + (checkzd2.Checked ? 1 : 0) + "#" + (checkzd3.Checked ? 1 : 0) + "#" + (checkzd4.Checked ? 1 : 0);
            //认知障碍
            ff.RenZhiZhangAi = (checkrz1.Checked ? 1 : 0) + "#" + (checkrz2.Checked ? 1 : 0) + "#" + (checkrz3.Checked ? 1 : 0);
            //环境因素
            ff.HuanJingYinSu = (checkhj1.Checked ? 1 : 0) + "#" + (checkhj2.Checked ? 1 : 0) + "#" + (checkhj3.Checked ? 1 : 0) + "#" + (checkhj4.Checked ? 1 : 0);
            //对手术镇静麻醉的反应
            ff.DuiShouShuZhenJingMaZuiDeFanYing = (checkmz1.Checked ? 1 : 0) + "#" + (checkmz2.Checked ? 1 : 0) + "#" + (checkmz3.Checked ? 1 : 0);
            //药物应用
            ff.YaoWuYingYong = (checkyw1.Checked ? 1 : 0) + "#" + (checkyw2.Checked ? 1 : 0) + "#" + (checkyw3.Checked ? 1 : 0);
            //总分
            ff.ZongDeFen = textzongfen.Text;
            //评估人
            if (string.IsNullOrEmpty(this.Id))
                ff.PingGuRen = this.LoginUser.UserName;
            else
                ff.PingGuRen = textpingguren.Text;

            //表单ID
            ff.FormId = (int)FormType.FallFRA;
            //表单名字
            ff.FormName = EnumHelper.GetDescription(FormType.FallFRA);
            //表单ID
            record.FormId = (int)FormType.FallFRA;
            //表单名字
            record.FormName = EnumHelper.GetDescription(FormType.FallFRA);
            //住院号
            record.PatientId = Patientinfo.PATIENT_ID;
            //住院次数
            record.VisitId = Patientinfo.VISIT_ID;
            //患者姓名
            record.PatientName = Patientinfo.PATIENT_NAME;
            //床号
            record.BedNo = Patientinfo.BED_NO;
            //当前患者科室ID
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;
            //当前患者科室名称
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;
            //当前患者病区ID
            record.AreaId = Patientinfo.NURSE_CELL_CODE;
            //当前患者病区名称
            record.AreaName = Patientinfo.NURSE_CELL_NAME;

            //时间
            record.AssessmentDate = Convert.ToDateTime(dateEditpingguriqi.Text + " " + DateTime.Now.ToString("HH:mm:ss"));
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(ff, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                if (Base.ESSwitch.Equals("on"))//电签开始
                {
                    string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>{0}</sign_sys><title>{1}</title><patient_id>{2}</patient_id><patient_name>{3}</patient_name><emr_id>{4}</emr_id><emr_catalog>{5}</emr_catalog><his_id>{6}</his_id><event_no>{7}</event_no><visit_number>{8}</visit_number><dept_name>{9}</dept_name><prescribe_number>{10}</prescribe_number><prescribe_sub_number>{11}</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                    extraInfo = string.Format(extraInfo, EnumHelper.GetDescription(FormType.FallFRA) + "-" + (record.Id > 0 ? "更新" : "新增"), EnumHelper.GetDescription(FormType.FallFRA), record.PatientId, record.PatientName, record.Id, record.FormId, "", record.PatientId, record.VisitId, LoginUser.DepartmentName, record.PatientId, record.PatientId);
                    ElectronicSignature es = new ElectronicSignature(LoginUser);
                    bool res = es.GetElectronicSignatureImage(es, record.NursingFormData, extraInfo, record.FormId.ToString(), record.FormName, Patientinfo.PATIENT_ID, Patientinfo.VISIT_ID);
                    if (!res) { MessageBox.Show("电子签名失败"); return; }
                }//电签结束
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }

        }
        #endregion

        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

    }
}